Editable List box in HTML
I have recently came across a requirement where a list of objects need to be added and edited. That object has only two fields which is name & id. So there is no point of using a editable data table as well. Following is the solution I came across.
- Create a List box
- Add a text box and a button which will add new items to the list box.
- On click on list box item load a pop up which allows user to edit the items.
Code for generating Listbox at the View
- <div>
- <selectsize="10" id="workAreas" multiple>
- @foreach (var item in Model.WorkAreas)
- {
- <option id="option_@item.Id" value="@item.Id" onclick="new RegionManagement().OnWorkAreaEdit(@item.Id);" class="TextBoxLarge">
- @item.Name
- </option>
- }
- </select>
- </div>
Code for Input text box and button
- <div>
- <input type="text" id="txtWorkArea" />
- <button id="btAddWorkArea" class="btn btn-success" onclick="new RegionManagement().AddNewWorkArea();">+</button>
- </div>
Code for pop up
- div id="DIV_EditWorkArea" style="display: none;">
- <div id="DIV_EditWorkAreaSubForm" class="InnerEntrySubForm" style="display: none; height:200px; margin-top:-175px;">
- <div class="InneEntryData" id="DIV_EditWorkAreaEditInner">
- <input type="hidden"id="txtWorkAreaId" />
- <input type="text" id="txtEditWorkArea" />
- <span id="btEditWorkAreaOK" class="btn btn-success" onclick="new RegionManagement().SaveWorkAreavalue();">OK</span>
- <span id="btEditWorkAreaCancel" class="btn btn-warning " onclick="new RegionManagement().HideEditWorkArea();">X</span>
- </div>
- </div>
- </div>
Javascript
- this.OnWorkAreaEdit = function(id) {
- var txt = $('#option_' + id).text().trim();
- $('#txtEditWorkArea').val(txt);
- $('#txtWorkAreaId').val(id);
- $("#DIV_EditWorkArea").css('display', 'block');
- show("DIV_EditWorkAreaSubForm");
- };
- this.SaveWorkAreavalue = function() {
- var id = $('#txtWorkAreaId').val();
- var text = $('#txtEditWorkArea').val();
- $('#option_' + id).text(text);
- this.HideEditWorkArea();
- };
- this.AddNewWorkArea = function() {
- var uniqueId = Date.now();
- var text = $('#txtWorkArea').val();
- if (text != '') {
- $('#workAreas')
- .append('<option value="0" id="option_' + uniqueId + '"onclick="new RegionManagement().OnWorkAreaEdit(' + uniqueId + ');">' + text + '</option>');
- $('#txtWorkArea').val('');
- }
- };
Hope this helps.
Happy Coding !!!!!!
This comment has been removed by a blog administrator.
ReplyDelete